package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.business.GetClassroomTrackingStatsDelegate;
	import mx.rpc.events.ResultEvent;
	import com.welsy.stats.event.GetClassroomTrackingStatsEvent;
	import com.welsy.stats.vo.IterElementStatsVO;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.vo.IterStatsVO;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.view.classview.IterView;
	
	public class GetClassroomTrackingStatsCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetClassroomTrackingStatsDelegate = new GetClassroomTrackingStatsDelegate(this);
			var getClassroomTrackingStatsEvent : GetClassroomTrackingStatsEvent = GetClassroomTrackingStatsEvent(event);
			
			delegate.getClassroomTrackingStats(getClassroomTrackingStatsEvent.classroomId);
		}
		
		public function result(event : Object) :void {
			var xres : XMLList = XMLList(ResultEvent(event).result);
			
			var arNS : Namespace = new Namespace(NamespaceLocator.ADMINRESULTS_DATATYPES);
			
			var TAG_GETCLASSROOMTRACKINGSTATS : QName = new QName(arNS, "GetClassroomTrackingStatsResult");
			var TAG_CLASSROOMID : QName = new QName(arNS, "ClassroomId");
			var TAG_USERSCOUNT : QName = new QName(arNS, "UsersCount");
			
			var TAG_ITERSTATSCOLLECTION : QName = new QName(arNS, "IterStatsCollection");
			var TAG_ITERSTATSTYPE : QName = new QName(arNS, "IterStatsType");
			var TAG_ITERID : QName = new QName(arNS, "IterId");
			var TAG_TITLE : QName = new QName(arNS, "Title");
			var TAG_ITERELEMENTSTATSCOLLECTION : QName = new QName(arNS, "IterElementStatsCollection");
			var TAG_ITERELEMENTSTATSTYPE : QName = new QName(arNS, "IterElementStatsType");
			
			var TAG_ITERELEMENTID : QName = new QName(arNS, "IterElementId");
			var TAG_TESTSCOMPLETED : QName = new QName(arNS, "TestsCompleted");
			var TAG_TESTSPASSED : QName = new QName(arNS, "TestsPassed");
			
			var gctsr_e : XMLList = xres.descendants(TAG_GETCLASSROOMTRACKINGSTATS);
			smodel.users = gctsr_e.child(TAG_USERSCOUNT);
			
			var tricker : ArrayCollection = new ArrayCollection;
			var isc_e : XMLList = gctsr_e.descendants(TAG_ITERSTATSCOLLECTION).children();
			for each(var statsColl : XML in isc_e) {
				//IterStatsType
				var iterView : IterView = new IterView;
				var ivo : IterStatsVO = new IterStatsVO;
				ivo.iterId = statsColl.child(TAG_ITERID);
				ivo.title = statsColl.child(TAG_TITLE);
			
				var picker : ArrayCollection = new ArrayCollection;
				var stats_colls : XMLList = statsColl.child(TAG_ITERELEMENTSTATSCOLLECTION).children();
				for each(var elementStatsColl : XML in stats_colls) {
					//IterElementStatsType
					var iterElemView : CourseView = new CourseView;
					var ievo : IterElementStatsVO = new IterElementStatsVO;
					ievo.title = elementStatsColl.child(TAG_TITLE);
					ievo.iterElementId = elementStatsColl.child(TAG_ITERELEMENTID);
					ievo.testsCompleted = elementStatsColl.child(TAG_TESTSCOMPLETED);
					ievo.testsPassed = elementStatsColl.child(TAG_TESTSPASSED);
					iterElemView.iterElement = ievo;
					picker.addItem(iterElemView);
				}

				iterView.iterStats = ivo;
				iterView.courses = picker;
				iterView.refreshChildren();
				tricker.addItem(iterView);
			}
			smodel.iterList = tricker;
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetClassroomTrackingStatsCommand.fault!");
		}
		
	}
}